package com.wcfsy.wechat.wx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wcfsy.wechat.wx.entity.KeyWord;
import com.wcfsy.wechat.wx.pagination.IPage;
import com.wcfsy.wechat.wx.pagination.impl.Page;
import com.wcfsy.wechat.wx.sys.dao.WechatKeyWordDao;
import com.wcfsy.wechat.wx.util.DateStyle;
import com.wcfsy.wechat.wx.util.DateUtil;
import com.wcfsy.wechat.wx.util.JsonMessage;

@Controller
@RequestMapping("/cms/wechatkeyword")
public class WechatKeyWordController {
	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private WechatKeyWordDao wechatKeyWordDao;

	@RequestMapping("/view/page")
	public String page() {
		return "keyword/keyWordList";
	}

	@RequestMapping("/view")
	@ResponseBody
	public IPage<KeyWord> view(int page, int rows) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("index", page);
		paramMap.put("size", rows);
		paramMap.put("limit", "limit");
		return new Page<KeyWord>(wechatKeyWordDao.findByParam(paramMap),
				wechatKeyWordDao.count(paramMap), page, rows);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public JsonMessage delete(int id) {
		try {
			wechatKeyWordDao.deleteById(id);
			return JsonMessage.getSuccessJson("删除成功");
		} catch (Exception e) {
			return JsonMessage.getErrorJson("删除失败");
		}
	}

	@RequestMapping("/modify/page")
	public String preModify(int id, ModelMap modelMap) {
		modelMap.put("keyWord", wechatKeyWordDao.getById(id));
		return "keyword/editKeyWord";
	}

	@RequestMapping("/add/page")
	public String preAdd(ModelMap modelMap) {
		return "keyword/addKeyWord";
	}

	@RequestMapping("/add")
	@ResponseBody
	public JsonMessage add(KeyWord keyWord) {
		keyWord.setCreateTime(DateUtil.DateToString(new Date(),
				DateStyle.YYYY_MM_DD_HH_MM_SS));
		try {
			wechatKeyWordDao.add(keyWord);
			return JsonMessage.getSuccessJson("添加成功");
		} catch (Exception e) {
			return JsonMessage.getErrorJson("添加失败");
		}
	}

	 
	@RequestMapping("/modify")
	@ResponseBody
	public JsonMessage modify(KeyWord keyWord) {
		try {
			wechatKeyWordDao.update(keyWord);
			return JsonMessage.getSuccessJson("修改成功");
		} catch (Exception e) {
			return JsonMessage.getErrorJson("修改失败");
		}
	}
}
